package nige.portable.commun;

import java.util.Random;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class MyCanvas extends Canvas implements CommandListener {
	public static String AFF = "";
	public static String AFF2 = "";
	int nbPoly=10;
	int maxTime = 0;
	
	Graphics buffer = null;
	Image image = null;
	
	public void Canvas() {
		setCommandListener(this);
	}
	public void paint(Graphics g) {
		if(buffer==null){
			image = Image.createImage(getWidth(),getHeight());
			buffer = image.getGraphics();
		}
		buffer.setColor(Utils.getColor(255,255,255));
		buffer.fillRect(0,0,getWidth(),getHeight());
		Random ran = new Random();
		buffer.setColor(0);
		int delt = Utils.getInstant();
		for(int i=0;i<nbPoly;i++) {
			buffer.drawRect(ran.nextInt(getWidth()),ran.nextInt(getHeight()),
					0,0);
		}
		if(maxTime<Utils.getInstant()-delt) {
			maxTime = Utils.getInstant()-delt;
		}
		buffer.drawString("Time : "+(maxTime)+"ms",
				getWidth()/3,getHeight()/3,0);
		buffer.drawString("Polygons : "+(nbPoly),
				getWidth()/3,getHeight()/3+50,0);
		try {
			Image img = Image.createImage("C:/tmp/la-mort.gif");
			buffer.drawImage(img,1,1,0);
		}
		catch(Exception e) {
			System.out.println(e);
		}
		g.drawImage(image,0,0,0);
	}
	public void commandAction(Command arg0, Displayable arg1) {
	}
	public void keyPressed(int code) {
		maxTime = 0;
		switch(code) {
			case 50:
				nbPoly += 10;
				break;
			case 56:
				nbPoly -= 10;
				break;
			case 54:
				nbPoly *= 10;
				break;
			case 52:
				nbPoly /= 10;
				break;
		}
	}
	public void keyReleased(int code) {
	}
}
